package com.atguigu.springcloud.service.impl;

import com.atguigu.springcloud.service.IMessageProvider;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 消息驱动生产者实现类
 * 这里这里实现类不用加@Service注解，只操作绑定
 *
 * @author Tu
 * @date 2023/11/2 17:21
 */
//EnableBinding可以理解为通道，定义消息推送管道
@EnableBinding(Source.class)
public class ImessageProviderImpl implements IMessageProvider {

    //定义消息发送管道，注意这里名称一定为output(同bean容器中名称)，不是的话需要(name="output")指定
    @Resource(name = "output")
    private MessageChannel messageChannel;

    @Override
    public String send() {
        //生成流水号
        String serialNo = UUID.randomUUID().toString();
        //注意不要引入错包integration..  构建并通过管道发送消息
        messageChannel.send(MessageBuilder.withPayload(serialNo).build());
        System.out.println("*******" + serialNo + "*******");
        return serialNo;
    }

}
